package com.begye.smartloan.apptest.kefu.adapter

import android.annotation.SuppressLint
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.begye.smartloan.apptest.R
import com.util.utillib.extend.copyLabel
import com.util.utillib.extend.openDial
import org.json.JSONObject

class KeFuDialogAdapter(var dataList: List<JSONObject>) :
    RecyclerView.Adapter<KeFuDialogAdapter.ViewHolder>() {

    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int,
    ): ViewHolder {
        val view = LayoutInflater.from(parent.context)
            .inflate(R.layout.list_item_ke_fu_dialog, parent, false)
        return ViewHolder(view)
    }

    inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        private val listItemKeFuDialogActionContent: LinearLayout =
            view.findViewById(R.id.listItemKeFuDialogActionContent)

        private val listItemKeFuDialogActionTitle: TextView =
            view.findViewById(R.id.listItemKeFuDialogActionTitle)

        private val listItemKeFuDialogActionIcon: ImageView =
            view.findViewById(R.id.listItemKeFuDialogActionIcon)

        private val listItemKeFuDialogTitle: TextView =
            view.findViewById(R.id.listItemKeFuDialogTitle)

        private val listItemKeFuDialogDetail: TextView =
            view.findViewById(R.id.listItemKeFuDialogDetail)


        @SuppressLint("SetTextI18n")
        fun bindData(position: Int, item: JSONObject) {
            listItemKeFuDialogDetail.text = item.optString("value", "")

            val type = item.optInt("type", 0)
            if (type == 0) {
                listItemKeFuDialogTitle.text = listItemKeFuDialogTitle.context.getString(
                    R.string.ke_fu_text_7,
                    (position + 1).toString()
                )
                listItemKeFuDialogActionTitle.text =
                    listItemKeFuDialogActionTitle.context.getString(R.string.ke_fu_text_4)
                listItemKeFuDialogActionIcon.setImageResource(R.drawable.dres_129)

                listItemKeFuDialogActionContent.setOnClickListener {
                    val text = listItemKeFuDialogDetail.text.toString().trim()
                    listItemKeFuDialogDetail.context.copyLabel(text, "Success")
                }
            } else {
                listItemKeFuDialogTitle.text = listItemKeFuDialogTitle.context.getString(
                    R.string.ke_fu_text_6,
                    (position + 1).toString()
                )
                listItemKeFuDialogActionTitle.text =
                    listItemKeFuDialogActionTitle.context.getString(R.string.ke_fu_text_5)
                listItemKeFuDialogActionIcon.setImageResource(R.drawable.dres_130)

                listItemKeFuDialogActionContent.setOnClickListener {
                    val text = listItemKeFuDialogDetail.text.toString().trim()
                    listItemKeFuDialogDetail.context.openDial(text)
                }
            }
        }
    }

    @SuppressLint("SetTextI18n")
    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.bindData(position, dataList[position])
    }

    override fun getItemCount(): Int {
        return dataList.size
    }
}